"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UploadReqHandler = void 0;
const AccountModel_1 = require("../account_server/model/AccountModel");
const UserDao_1 = require("../account_server/dao/UserDao");
const ErrorCode_1 = require("../src/config/ErrorCode");
const ServerConfig_1 = require("../src/config/ServerConfig");
const Logger_1 = require("../src/engine/utils/Logger");
const Md5Helper_1 = require("../src/engine/utils/Md5Helper");
const WebUtil_1 = require("../src/engine/utils/WebUtil");
const GameImgHelper_1 = require("../src/game/helper/GameImgHelper");
const ResponseModel_1 = require("../src/model/ResponseModel");
const ServerManager_1 = require("../src/server/ServerManager");
const request = require('request');
const crypto = require("crypto");
const multer = require('multer');
const routerMap = {
    updateServerStatus: "/updateServerStatus",
    upload_img: "/upload_img", //上传图片
};
class UploadReqHandler {
    //     private versionStr: string =
    //         `
    //     {
    //         "iv": "1.0.0.100",
    //         "av": "1.0.0.101",
    //         "al": "http://ibirdgame.cn:9604/202109245.apk",
    //         "il": "http://ibirdgame.cn:9604/202109245.apk"
    //     }
    // `
    init(app) {
        for (const key in routerMap) {
            if (routerMap[key] == routerMap.upload_img) {
                app.post(routerMap.upload_img, multer().single('file'), this.on_upload_img.bind(this));
            }
            else {
                app.use(routerMap[key], this.onBaseRoot.bind(this));
            }
        }
    }
    onBaseRoot(req, resp) {
        try {
            let ip = WebUtil_1.WebUtil.getIpByExpressReq(req);
            Logger_1.Logger.log("onBaseRoot=req=", ip, req.baseUrl);
            if (req.baseUrl == routerMap.updateServerStatus) {
                ServerConfig_1.ServerConfig.initConfig(ServerConfig_1.ServerConfig.nowServerConfig.nowPort);
                resp.end("success");
            }
        }
        catch (error) {
            Logger_1.Logger.warn("onBaseRoot error", error);
        }
    }
    checkBody(ip, req, resp, callback) {
        if (req.body) {
            try {
                let jsonObj = JSON.parse(req.body);
                let k = jsonObj["k"];
                let data = jsonObj["data"];
                if (k == Md5Helper_1.Md5Helper.getMd5_encrypt(JSON.stringify(data))) {
                    callback && callback(req, resp, data, ip);
                }
                else {
                    Logger_1.Logger.warn("收到非法Http请求 key", ip, req.body);
                    resp.end();
                }
            }
            catch (error) {
                Logger_1.Logger.warn("收到非法Http请求body", ip, req.body, error);
                resp.end();
            }
        }
        else {
            resp.end();
        }
    }
    on_upload_img(req, resp) {
        return __awaiter(this, void 0, void 0, function* () {
            let ip = WebUtil_1.WebUtil.getIpByExpressReq(req);
            let k = req.body["k"];
            let data = req.body["data"];
            Logger_1.Logger.log("on_upload_img==ip=", ip, k, data);
            let respModel = new ResponseModel_1.ResponseModel();
            if (k == Md5Helper_1.Md5Helper.getMd5_encrypt(data)) {
                respModel.code = ErrorCode_1.ErrorCode.Code.Suc;
                try {
                    let dataObj = JSON.parse(data);
                    let userid = dataObj["userid"];
                    let uploadType = dataObj["uploadType"];
                    let sign = dataObj["sign"];
                    let account = dataObj["account"];
                    if (req.file) {
                        let respModel = yield AccountModel_1.AccountModel.login_async(account, sign);
                        // Logger.warn("on_upload_img==ip=====111=", userid, account,  sign)
                        if (respModel.code == ErrorCode_1.ErrorCode.Code.Suc) {
                            let userData = yield UserDao_1.UserDao.get_user_data_by_account(account);
                            // Logger.warn("on_upload_img==ip=====2222=", userid, account,  sign)
                            if (userData) {
                                // Logger.warn("on_upload_img==ip=====44444=",data, userid)
                                if (userid == userData["userid"]) {
                                    let buffer = req.file.buffer;
                                    let imgurl = GameImgHelper_1.GameImgHelper.buffer_to_imgurl(userid, uploadType, buffer);
                                    // respModel = await HallServer_SocketMsgHandler.saveUploadImg(imgurl, uploadType, userid, data["login_type"])
                                    // Logger.warn("on_upload_img==ip=====5555=",data, userid)
                                    if (respModel.code == ErrorCode_1.ErrorCode.Code.Suc) {
                                        ServerManager_1.ServerManager.instance.notify_update_img(userid, imgurl, uploadType);
                                    }
                                }
                                else {
                                    respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                                    respModel.msg = "用户id不对";
                                }
                            }
                            else {
                                respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                                respModel.msg = "账号玩家不存在";
                            }
                            resp.end(JSON.stringify(respModel));
                        }
                        else {
                            resp.end(JSON.stringify(respModel));
                        }
                    }
                    else {
                        respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                        respModel.msg = "on_upload_img==上传文件不存在";
                        resp.end(JSON.stringify(respModel));
                    }
                }
                catch (error) {
                    Logger_1.Logger.warn("error==", error);
                    respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                    respModel.msg = "data不是json";
                    resp.end(JSON.stringify(respModel));
                }
            }
            else {
                respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                respModel.msg = "签名错误";
                resp.end(JSON.stringify(respModel));
            }
        });
    }
}
exports.UploadReqHandler = UploadReqHandler;
UploadReqHandler.instance = new UploadReqHandler();
